package com.workerhelping.controller;

import com.workerhelping.elasticsearchEntity.ESAssistVO;
import com.workerhelping.elasticsearchEntity.ESRecordVO;
import com.workerhelping.feignClients.ESFeignClient;
import com.workerhelping.result.Result;
import com.workerhelping.result.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/expend")
public class ExpendController {

    @Autowired
    private ESFeignClient esFeignClient;

    /**
     * 搜索所有救助信息
     * - 聚合查询
     * @param startDate 开始日期：区间搜索
     * @param endDate 结束日期：区间搜索
     * @param minPrice 最小金额：区间搜搜
     * @param maxPrice 最大金额：区间搜搜
     * @param year 资金年度：精确查询
     * @param category 资金类别：精准查询
     * @param workerId 员工id：精准查询
     * @return
     */
    @GetMapping("/search")
    public Result<List<ESAssistVO>> searchAssist(@RequestParam(value = "startDate", required = false) String startDate,
                                                 @RequestParam(value = "endDate", required = false) String endDate,
                                                 @RequestParam(value = "minPrice", required = false) Double minPrice,
                                                 @RequestParam(value = "maxPrice", required = false) Double maxPrice,
                                                 @RequestParam(value = "sYear",required = false) String year,
                                                 @RequestParam(value = "sCategory", required = false) String category,
                                                 @RequestParam(value = "workerId", required = false) Integer workerId) {
        Result<List<ESAssistVO>> result = esFeignClient.searchAssist(startDate, endDate, minPrice, maxPrice,year, category, workerId);
        if (!result.isOk())
            return Result.fail();
        List<ESAssistVO> assists = result.getData();
        if (assists.isEmpty())
            return Result.build(null, ResultCodeEnum.DATA_IS_EMPTY);
        return Result.ok(assists);
    }
}
